package LinkedList;

import com.sun.org.apache.xerces.internal.xs.ShortList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2023-12-03
 * Time: 13:57
 */
public class MyLinkedList implements Ilist{
    static class ListNode {
        public int value;
        public ListNode prev;
        public ListNode next;

        public ListNode(int value) {
            this.value = value;
        }
    }
    //定义一个标记头节点的引用变量
    public ListNode head;
    //定义一个标记尾节点的引用变量
    public ListNode last;
    //头插法
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //如果链表为空
        if(head == null) {
            head = node;
            last = node;
        }
        else {
            //链表不为空
            node.next = head;
            head.prev = node;
            node.prev = null;
            head = node;
        }
    }
    //尾插法
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //如果链表为空
        if(head == null) {
            head = node;
            last = node;
        }
        else {
            //链表不为空
            last.next = node;
            node.prev = last;
            node.next = null;
            last = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {
        //先判断index合不合法
        if(index<0 || index >size()) {
            throw new IndexException("位置不合法:"+index);
        }
        //1.从头插入，头插法
        if(index == 0) {
            addFirst(data);
            return;
        }
        //2.从尾插，尾插法
        if(index == size()) {
            addLast(data);
            return;
        }
        //3.从中间插
        //找要插入的位置
        ListNode cur = findAddIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    private ListNode findAddIndex(int index) {
        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
       ListNode cur = head;
       while(cur != null) {
           if(cur.value == key) {
               return true;
           }
           else {
               cur = cur.next;
           }
       }
       return false;
    }
    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.value == key) {
                if(cur == head) {
                    //删除头节点
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        //只有一个节点 且是需要删除的节点
                        last = null;
                    }
                }else {
                    //删除中间节点
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }else {
                        //删除尾巴节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    @Override
    public void removeAllKeye(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        //只有一个节点 且是需要删除的节点
                        last = null;
                    }
                }else {
                    //删除中间节点
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }else {
                        //删除尾巴节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到双链表的长度
    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //清除链表
    @Override
    public void clear() {
    head =null;
    last =null;
    }
    //打印链表
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
